#ifndef ALERTMANAGEMENT_H
#define ALERTMANAGEMENT_H

#include <QWidget>
#include <QStandardItemModel>
#include <QTimer>
#include <QMenu>
#include <QFormLayout>
#include <QSpinBox>
#include <QCheckBox>
#include <QDialogButtonBox>
#include "databasemanager.h"
#include "systemsetting.h"

namespace Ui {
class AlertManagement;
}

class AlertManagement : public QWidget
{
    Q_OBJECT

public:
    explicit AlertManagement(QWidget *parent = nullptr);
    ~AlertManagement();

private slots:
    void loadRules();
    void loadRecords();
    void onAddRule();
    void onEditRule();
    void onDeleteRule();
    void onQueryRecords();
    void onExportRecords();
    void onMarkAsProcessed();
    void onDeviceFilterChanged(int index);
    void onStatusFilterChanged(int index);

    // 右键菜单槽函数
    void showRulesContextMenu(const QPoint &pos);
    void showRecordsContextMenu(const QPoint &pos);
    void deleteSelectedRecord();
    void initDeviceFilter();

private:
    Ui::AlertManagement *ui;
    QStandardItemModel *m_rulesModel;
    QStandardItemModel *m_recordsModel;
    QTimer *m_checkTimer;
    DatabaseManager* dbManager;

    void setupModels();
    void setupConnections();
    void checkAlarms();
    void triggerAlarm(int deviceId, const QString &content);
    void sendNotification(const QString &action, const QString &message);
    QStringList parseQueryResponse(const QString &response);
};

#endif // ALERTMANAGEMENT_H
